import { createRouter, createWebHistory } from 'vue-router'
import { ElMessage } from 'element-plus'
import Home from '../views/Home.vue'
import SearchResult from '../views/SearchResult.vue'
import NewsDetail from '../views/NewsDetail.vue'
import Login from "../views/Login.vue";
import Sign from "../views/Sign.vue";
import Aboutus from "@/views/Aboutus.vue";
import PublishNews from "../views/PublishNews.vue"
import PersonalCenter from '@/views/PersonalCenter.vue';
import NewsReview from '@/views/NewsReview.vue';
import DraftManagement from '@/views/DraftManagement.vue';
import TestAPI from '@/views/TestAPI.vue';
import NewsReviewPersonal from '@/views/personal/NewsReviewPersonal.vue'
import UserProfile from '@/views/personal/UserProfile.vue'

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/search',
    name: 'SearchResult',
    component: SearchResult,
    meta: {
      ishide: false
    }
  },
  {
    path: '/news/:id',
    name: 'NewsDetail',
    component: NewsDetail
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/sign',
    name: 'Sign',
    component: Sign
  },
  {
    path: '/aboutus',
    name: 'Aboutus',
    component: Aboutus
  },
  {
    path: '/publish/news',
    name: 'PublishNews',
    component: PublishNews,
    meta: {
      requiresAuth: true // 需要登录才能访问
    }
  },
  {
    path: '/personalCenter',
    name: 'PersonalCenter',
    component: PersonalCenter,
    meta: {
      requiresAuth: true // 需要登录才能访问
    }
  },
  {
    //进入个人作品详情页面
    path: '/newsPersonal/:id',
    name: 'newsPersonal',
    component: NewsReviewPersonal,
    props: true,
    meta: {
      requiresAuth: true // 需要登录才能访问
    }
  },
  {
    path: '/news/review',
    name: 'NewsReview',
    component: NewsReview,
    meta: {
      requiresAuth: true,
      requiresAdmin: true // 需要管理员权限
    }
  },
  {
    path: '/draft/management',
    name: 'DraftManagement',
    component: DraftManagement,
    meta: {
      requiresAuth: true // 需要登录才能访问
    }
  },
  {
    path: '/test/api',
    name: 'TestAPI',
    component: TestAPI
  },
  {
    path: '/messages/system',
    name: 'SystemMessage',
    component: () => import('@/views/SystemMessage.vue'),
    meta: {
      requiresAuth: true,
      title: '系统通知'
    }
  },
  {
    path: '/messages/comment',
    name: 'SystemComment',
    component: () => import('@/views/SystemComment.vue'),
    meta: {
      requiresAuth: true,
      title: '评论通知'
    }
  },
  {
    path: '/messages/likes',
    name: 'SystemLikes',
    component: () => import('@/views/SystemLikes.vue'),
    meta: {
      requiresAuth: true,
      title: '点赞通知'
    }
  },
  {
    path: '/messages/likes',
    name: '  UserProfile',
    component: () => import('@/views/SystemLikes.vue'),
    meta: {
      requiresAuth: true,
      title: '点赞通知'
    }
  },
    {
    path: '/userfile/:id',
    name: 'UserProfile',
    component: UserProfile,
      meta: {
        title: '用户主页' // 取消注释，确保标题显示
      }
  },
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  const userType = localStorage.getItem('userType')

  if (to.meta.requiresAuth && !token) {
    // 需要登录但未登录，重定向到登录页
    next({
      path: '/',
      query: { redirect: to.fullPath } // 保存原目标路径
    })
  } else if (to.meta.requiresAdmin && userType !== '0') {
    // 需要管理员权限但用户不是管理员
    ElMessage.error('您没有权限访问此页面')
    next('/')
  } else {
    next()
  }
})

export default router
